//
//  GYNewChooseView.m
//  GYDemo
//
//  Created by Mrzhang on 2018/6/25.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYNewChooseView.h"
#import "Head.h"
#import "GYTypeCollectView.h"
@interface GYNewChooseView ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property(nonatomic,strong)UIButton* orderBtn;
@property(nonatomic,strong)UIButton* priceBtn;
@property(nonatomic,strong)UITextField* minPrice;
@property(nonatomic,strong)UITextField* maxPrice;
@property(nonatomic,strong)UIButton* typeBtn;
@property(nonatomic,strong)UIImageView * lineIV;
@property(nonatomic,strong)GCBaseTableView* chooseTable;

@property(nonatomic,strong)UIView* displayView;
@property(nonatomic,assign)NSInteger selectRow;
@property(nonatomic,strong)UIView* TypePlayView;
@end

@implementation GYNewChooseView
-(GYTypeCollectView *)typeCollectView
{
    if (!_typeCollectView) {
        _typeCollectView=[[GYTypeCollectView alloc] initWithFrame:CGRectMake(ScreenWidth, 0, WFCGFloatX(375-65), ScreenHeight)];
        _typeCollectView.backgroundColor=[UIColor whiteColor];
        @WeakSelf;

        _typeCollectView.Handler = ^(NSDictionary *paraDic) {
            selfp.backHandler(1, paraDic);
            [selfp missAllView];
        };
        
    }
    return _typeCollectView;
}
-(UIView *)TypePlayView{
    
    if (!_TypePlayView) {
        _TypePlayView=[[UIView alloc] init];
        _TypePlayView.backgroundColor=[UIColor blackColor];
        _TypePlayView.alpha=.3;
        UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(missAllView)];
        tap.numberOfTapsRequired=1;
        [_TypePlayView addGestureRecognizer:tap];
        _TypePlayView.userInteractionEnabled=YES;
        _TypePlayView.y = 0;
        _TypePlayView.x = 0;
        _TypePlayView.width = ScreenWidth;
        _TypePlayView.height = ScreenHeight;
        _TypePlayView.hidden = YES;
    }
    return _TypePlayView;
}
-(UIView *)displayView{
    
    if (!_displayView) {
        _displayView=[[UIView alloc] init];
        _displayView.backgroundColor=[UIColor blackColor];
        _displayView.alpha=.3;
        UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(missAllView)];
        tap.numberOfTapsRequired=1;
        [_displayView addGestureRecognizer:tap];
        _displayView.userInteractionEnabled=YES;
        _displayView.y = self.bottom;
        _displayView.width = self.width;
        _displayView.height = WF_UI_VIEW_HEIGHT;
        _displayView.hidden = YES;
    }
    return _displayView;
}
-(GCBaseTableView *)chooseTable
{
    if (!_chooseTable) {
        
        _chooseTable=[[GCBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _chooseTable.frame=WFCGRectMake(0, 0, 375, 0);
        _chooseTable.dataSource=self;
        _chooseTable.delegate=self;
        [_chooseTable setSeparatorColor: Main_Color_BG];
        _chooseTable.showsVerticalScrollIndicator=NO;
        _chooseTable.scrollEnabled=NO;
        _chooseTable.hidden=YES;
    }
    return _chooseTable;
}
-(UIImageView *)lineIV
{
    if (!_lineIV) {
        _lineIV=[[UIImageView alloc] init];
        _lineIV.backgroundColor=HEXCOLOR(@"#333333");
        _lineIV.x=WFCGFloatX(232);
        _lineIV.y=WFCGFloatY(20);
        _lineIV.width=WFCGFloatX(9);
        _lineIV.height=WFCGFloatY(1);
    }
    return _lineIV;
}
-(UIButton *)orderBtn
{
    if (!_orderBtn) {
        _orderBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_orderBtn setTitle:@"综合排序" forState:UIControlStateNormal];
        [_orderBtn setTitleColor:HEXCOLOR(@"#333333") forState:UIControlStateNormal];
        _orderBtn.titleLabel.font=SYSTEMFONT(14);
        _orderBtn.x=WFCGFloatX(7);
        _orderBtn.y=0;
        _orderBtn.width=WFCGFloatX(70);
        _orderBtn.height=WFCGFloatY(40);
        [_orderBtn addTarget:self action:@selector(showView:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView* locationIV=[[UIImageView alloc] init];
        locationIV.image=[UIImage imageNamed:@"list_order_mark"];
        locationIV.width=locationIV.image.size.width;
        locationIV.height=locationIV.image.size.height;
        locationIV.x=WFCGFloatX(65);
        locationIV.y=WFCGFloatY(40-locationIV.height)/2;
        [_orderBtn addSubview:locationIV];
        
    }
    return _orderBtn;
}
-(UIButton *)priceBtn
{
    if (!_priceBtn) {
        _priceBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_priceBtn setTitle:@"价格" forState:UIControlStateNormal];
        [_priceBtn setTitleColor:HEXCOLOR(@"#666666") forState:UIControlStateNormal];
        _priceBtn.titleLabel.font=SYSTEMFONT(14);
        _priceBtn.x=WFCGFloatX(105);
        _priceBtn.y=0;
        _priceBtn.width=WFCGFloatX(42);
        _priceBtn.height=WFCGFloatY(40);
        [_priceBtn addTarget:self action:@selector(priceOrder:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView* locationIV=[[UIImageView alloc] init];
        locationIV.image=[UIImage imageNamed:@"list_price_mark"];
        locationIV.width=locationIV.image.size.width;
        locationIV.height=locationIV.image.size.height;
        locationIV.x=WFCGFloatX(39);
        locationIV.y=WFCGFloatY(40-locationIV.height)/2;
        [_priceBtn addSubview:locationIV];
    }
    return _priceBtn;
}
-(UIButton *)typeBtn
{
    if (!_typeBtn) {
        _typeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_typeBtn setTitle:@"筛选" forState:UIControlStateNormal];
        [_typeBtn setTitleColor:HEXCOLOR(@"#666666") forState:UIControlStateNormal];
        _typeBtn.titleLabel.font=SYSTEMFONT(14);
        _typeBtn.x=WFCGFloatX(312);
        _typeBtn.y=0;
        _typeBtn.width=WFCGFloatX(63);
        _typeBtn.height=WFCGFloatY(40);
        @WeakSelf;
        [_typeBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            [UIView animateWithDuration:0.3 animations:^{
                [selfp.TypePlayView setHidden:NO];
                selfp.typeCollectView.x=WFCGFloatX(65);
            }];
        }];
        
    }
    return _typeBtn;
}
-(UITextField *)minPrice
{
    if (!_minPrice) {
        _minPrice=[[UITextField alloc] init];
        _minPrice.placeholder=@"最低价";
        _minPrice.textColor=HEXCOLOR(@"#666666");
        _minPrice.font=SYSTEMFONT(11);
        _minPrice.backgroundColor=HEXCOLOR(@"#F7F7F7");
        _minPrice.x=WFCGFloatX(173);
        _minPrice.y=WFCGFloatY(8);
        _minPrice.width=WFCGFloatX(52);
        _minPrice.height=WFCGFloatY(25);
        _minPrice.textAlignment=NSTextAlignmentCenter;
        _minPrice.keyboardType=UIKeyboardTypeNumberPad;
        _minPrice.returnKeyType=UIReturnKeyNext;
        [WFFunctions WFUIaddbordertoView:_minPrice radius:2 width:0 color:[UIColor clearColor]];
        _minPrice.delegate=self;
        
    }
    return _minPrice;
}
-(UITextField *)maxPrice
{
    if (!_maxPrice) {
        _maxPrice=[[UITextField alloc] init];
        _maxPrice.placeholder=@"最高价";
        _maxPrice.textColor=HEXCOLOR(@"#666666");
        _maxPrice.font=SYSTEMFONT(11);
        _maxPrice.backgroundColor=HEXCOLOR(@"#F7F7F7");
        _maxPrice.x=WFCGFloatX(247);
        _maxPrice.y=WFCGFloatY(8);
        _maxPrice.width=WFCGFloatX(52);
        _maxPrice.height=WFCGFloatY(25);
        _maxPrice.textAlignment=NSTextAlignmentCenter;
        _maxPrice.keyboardType=UIKeyboardTypeNumberPad;
        _maxPrice.returnKeyType=UIReturnKeySearch;
        [WFFunctions WFUIaddbordertoView:_maxPrice radius:2 width:0 color:[UIColor clearColor]];
        _maxPrice.delegate=self;
        
    }
    return _maxPrice;
}
-(instancetype)init
{
    self=[super init];
    if (self) {
        _selectRow=0;
        [self addSubview:self.orderBtn];
        [self addSubview:self.priceBtn];
        [self addSubview:self.minPrice];
        [self addSubview:self.lineIV];
        [self addSubview:self.maxPrice];
        [self addSubview:self.typeBtn];
        [[[UIApplication sharedApplication]delegate].window addSubview:self.TypePlayView];
        [[[UIApplication sharedApplication]delegate].window addSubview:self.typeCollectView];
        
        
        
    }
    return self;
}
-(void)priceOrder:(UIButton*)sender
{
    sender.selected=!sender.selected;
    if (sender.selected) {
        self.backHandler(2, @{@"sort":@"2"});
    }else
    {
        self.backHandler(2, @{@"sort":@"3"});
    }
    
}
-(void)reqeustData:(NSString *)typeID viewType:(NSInteger)type
{
    [self.typeCollectView requestSecondtype:typeID viewType:type];
}
-(void)layoutSubviews
{
    [super layoutSubviews];

    if (_superType==1)
    {
        [self.superview.superview addSubview:self.displayView];
        [self.superview.superview addSubview:self.chooseTable];
        [self.superview.superview bringSubviewToFront:self];
    }else
    {
        [self.superview addSubview:self.displayView];
        [self.superview addSubview:self.chooseTable];
        [self.superview bringSubviewToFront:self];
    }
}
-(void)missAllView
{
    [self endEditing:YES];
    [UIView animateWithDuration:0.3 animations:^{

        _chooseTable.hidden=YES;
        _typeCollectView.x=ScreenWidth;
    } completion:^(BOOL finished) {
        _displayView.hidden = YES;
        _TypePlayView.hidden=YES;
    }];
}
-(void)showView:(UIButton*)sender
{
    _displayView.hidden = NO;
    _chooseTable.hidden=NO;
    _displayView.x=0;
    
    if (_superType==1) {
        _displayView.y=WFCGFloatY(203+123)+self.height;
        _chooseTable.y = WFCGFloatY(203+123)+self.height;
        _chooseTable.x=0;
        _chooseTable.height = WFCGFloatY(45*3);

    }else
    {
        _chooseTable.y = self.y+self.height;
        _chooseTable.x=0;
        
        CATransition *transition = [CATransition animation];
        transition.type = kCATransitionMoveIn;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.fillMode = kCAFillModeForwards;
        transition.duration = 0.3;
        transition.subtype = kCATransitionFromBottom;
        [[_chooseTable layer] addAnimation:transition forKey:@"UITableViewReloadDataAnimationKey"];
        _chooseTable.height = WFCGFloatY(45*3);
    }
    [_chooseTable reloadData];
}
#pragma mark - table delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"section2cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    [cell.contentView removeAllSubviews];
    
    UILabel* leftlab=[[UILabel alloc] init];
    leftlab.textAlignment=NSTextAlignmentLeft;
    leftlab.textColor=Main_Color_Black;
    leftlab.font=SYSTEMFONT(14);
    leftlab.frame=WFCGRectMake(15, 0, 70, 45);
    [cell.contentView addSubview:leftlab];
    
    leftlab.text=indexPath.row==0?@"综合排序":indexPath.row==1?@"信誉等级":@"销量";
    
    UIImageView* locationIV=[[UIImageView alloc] init];
    locationIV.image=[UIImage imageNamed:@"order_choose_mark"];
    locationIV.width=locationIV.image.size.width;
    locationIV.height=locationIV.image.size.height;
    locationIV.x=WFCGFloatX(336);
    locationIV.y=WFCGFloatY(45-locationIV.height)/2;
    [cell.contentView addSubview:locationIV];
    if (_selectRow==indexPath.row) {
        [locationIV setHidden:NO];
        leftlab.textColor=Main_Color_Red;
    }else
    {
        [locationIV setHidden:YES];
        leftlab.textColor=Main_Color_Black;
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WFCGFloatY(45);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectRow=indexPath.row;
     [_orderBtn setTitle:indexPath.row==0?@"综合排序":indexPath.row==1?@"信誉等级":@"销量" forState:UIControlStateNormal];
    
    if (indexPath.row==0)
    {
        
        self.backHandler(2, @{@"sort":@"1"});
    }
    if (indexPath.row==1)
    {
        self.backHandler(2, @{@"sort":@"5"});
    }
    if (indexPath.row==2)
    {
        self.backHandler(2, @{@"sort":@"4"});
    }
    [self missAllView];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField==_minPrice)
    {
        
    }
    if (textField==_maxPrice)
    {
        if ([_maxPrice.text integerValue]<[_minPrice.text integerValue])
        {
            [FTIndicator showErrorWithMessage:@"最高价不能低于最低价"];
        }else
        {
            NSString* str=[NSString stringWithFormat:@"%@,%@",_minPrice.text,_maxPrice.text];
            self.backHandler(3, @{@"minprice":_minPrice.text,@"maxprice":_maxPrice.text});
        }
    }
}
@end
